/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.school.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.cncqs.backend.company.entity.ComCompany;
import com.cncqs.backend.company.service.ComCompanyService;
import com.jeesite.common.service.ServiceException;
import com.jeesite.modules.common.MyResponseBody;
import com.jeesite.modules.utils.LatitudeUtils;
import org.apache.commons.collections.map.HashedMap;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.school.entity.School;
import com.jeesite.modules.school.service.SchoolService;

import java.util.HashMap;
import java.util.Map;

/**
 * schoolController
 * @author cxy
 * @version 2019-04-10
 */
@Controller
@RequestMapping(value = "${adminPath}/school/school")
public class SchoolController extends BaseController {

	@Autowired
	private SchoolService schoolService;
	@Autowired
	private ComCompanyService comCompanyService;

	@Value("${endpoint}")
	private String endpoint;

	@Value("${bucketName}")
	private String bucketName;

	/**
	 * 获取数据
	 */
	@ModelAttribute
	public School get(Long id, boolean isNewRecord) {
		return schoolService.get(id == null ? "": id.toString(), isNewRecord);
	}

	@RequestMapping("notLogin")
	@ResponseBody
	public MyResponseBody notLogin() {
		MyResponseBody responseBody = MyResponseBody.success();
		responseBody.setData("321312");
		return responseBody;
	}


	/**
	 * 查询列表
	 */
	@RequiresPermissions("school:school:view")
	@RequestMapping(value = {"list", ""})
	public String list(School school, Model model) {
		model.addAttribute("school", school);
		return "modules/school/schoolList";
	}

	/**
	 * 查询列表数据
	 */
	@RequiresPermissions("school:school:view")
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<School> listData(School school, HttpServletRequest request, HttpServletResponse response) {
		school.setPage(new Page<>(request, response));
		Page<School> page = schoolService.findPage(school);
		return page;
	}

	/**
	 * 查看编辑表单
	 */
	@RequiresPermissions("school:school:view")
	@RequestMapping(value = "form")
	public String form(School school, Model model) {
		model.addAttribute("school", school);
		return "modules/school/schoolForm";
	}

	/**
	 * 保存school
	 */
	@RequiresPermissions("school:school:edit")
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated School school) {
//		if (StringUtils.isBlank(school.getPictureUrl())) {
//			school.setPictureUrl(null);
//		}
//		if (StringUtils.isNotBlank(school.getPictureUrl())) {
//			String url = school.getPictureUrl();
//			String suffix = url.substring(url.length() - 7);
//			String prefix = url.substring(0,url.length() - 7);
//			Integer suffixInt = Integer.parseInt(suffix) + 1;
//			url = prefix + suffixInt;
//			school.setPictureUrl("https://" + bucketName + "." + endpoint + "/" + url);
//		}
		Map<String, String> geocodingLatitude = LatitudeUtils.getGeocodingLatitude(school.getProvince() + school.getCity() + school.getArea());
		try {
			school.setLongitude(geocodingLatitude.get("lng"));
			school.setLatitude(geocodingLatitude.get("lat"));
		} catch (Exception e) {
			throw new ServiceException("请输入正确的地址:省/市/区域");
		}
		schoolService.save(school);
		return renderResult(Global.TRUE, text("保存学校成功！"));
	}

	/**
	 * 停用school
	 */
	@RequiresPermissions("school:school:edit")
	@RequestMapping(value = "disable")
	@ResponseBody
	public String disable(School school) {
		school.setStatus(School.STATUS_DISABLE);
		schoolService.updateStatus(school);
		return renderResult(Global.TRUE, text("停用school成功"));
	}

	/**
	 * 启用school
	 */
	@RequiresPermissions("school:school:edit")
	@RequestMapping(value = "enable")
	@ResponseBody
	public String enable(School school) {
		school.setStatus(School.STATUS_NORMAL);
		schoolService.updateStatus(school);
		return renderResult(Global.TRUE, text("启用school成功"));
	}

	/**
	 * 删除school
	 */
	@RequiresPermissions("school:school:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(School school) {
		schoolService.delete(school);
		return renderResult(Global.TRUE, text("删除school成功！"));
	}


	/**
	 * 首页映射
	 * */
	@RequestMapping(value = {"index", ""})
	public String index(School school, Model model) {
		model.addAttribute("school", school);
		return "modules/school/index";
	}

	/**
	 * 首页数据
	 * */
	@RequestMapping("getIndexData")
	@ResponseBody
	public MyResponseBody getIndexData() {
		MyResponseBody success = MyResponseBody.success();
		ComCompany comCompany = new ComCompany();
		comCompany.setCheckStatus(0);
		Long waitPassCompanyCount = comCompanyService.findCount(comCompany);
		Map<String,Object> map = new HashMap<>(8);
		map.put("waitPassCompanyCount",waitPassCompanyCount);
		success.setData(map);
		return success;
	}

}